package com.hjx.other;

/**
 * @author houjinxin
 * @version 创建时间：2019/11/1.
 */
public class SwapPairs {

    public ListNode swapPairs(ListNode head) {
        if (head == null) {
            return null;
        }
        if(head.next == null){
            return head;
        }
        ListNode nextHead = head.next.next;
        ListNode first = head;
        ListNode second = first.next;

        head = second;
        second = first;
        head.next = second;
        second.next = nextHead == null ? null : swapPairs(nextHead);
        return head;
    }

    public static void main(String[] args) {
        ListNode listNode1 = new ListNode(1);
        ListNode listNode2 = listNode1.next = new ListNode(2);
        ListNode listNode3 = listNode2.next = new ListNode(3);
        listNode3.next = new ListNode(4);
//        ListNode listNode1 = null;

        ListNode temp = new SwapPairs().swapPairs(listNode1);
        while (temp != null){
            System.out.println(temp.val);
            temp = temp.next;
        }
    }
}


